package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Files;
import com.ciei.dpagm.service.FilesService;
import com.ciei.dpagm.service.UsersService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.DateUtil;
import com.ciei.dpagm.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class FilesController extends BaseController {
	@Autowired
	private FilesService filesService;

	@Autowired
	private UsersService usersService;

	@RequestMapping("files/filesPage")
	public String filesPage(){
		return "files/filesPage";
	}

	@RequestMapping("files/filesUpload")
	public String filesUpload(){
		return "files/filesUpload";
	}

	/**
	 * 文件上传功能
	 * @author pp
	 * @date 2020/9/1
	 * @param request  文件
	 * @param id  id(例:供应商id)
	 * @param typeId typeId(附件类型id)
	 * @return
	 */
	@RequestMapping(value = "vue/files/upload")		//, method = RequestMethod.POST
	@ResponseBody
	public JSONObject uploadVue(HttpServletRequest request, String id,String typeId) {
		//System.out.println(id+typeId);
		return filesService.uploadFileVue(request, id,typeId);
		//return JsonUtil.getSuccess("ok");
	}

	/**
	 * 加载区域数据，以网格形式显示
	 * @return
	 */
	@RequestMapping("files/loadGrid")
	@ResponseBody
	public JSONObject loadGrid() {
		PageParam ps=getPageParams();
		Map<String, Object> params=ps.params;
		int posStart =ps.posStart;
		int count =ps.count;
		int totalCount=0;
		
		List<Files> lists = null;
		JSONObject json = new JSONObject();
		JSONArray jarr = new JSONArray();

		lists = filesService.findPage(posStart, count, params, new String[] {"fileName"}, new String[] {"deviceKey"},null);
		if(lists != null && lists.size() > 0) {
			int rowNumber=posStart;
			for (Files files : lists) {
				Map<String, Object> mobj = new HashMap<String, Object>();
				mobj.put("rowNumber",++rowNumber);
				mobj.put("id", files.getFileId());
				mobj.put("fileId", files.getFileId());
				mobj.put("fileName", files.getFileName());
				mobj.put("path", files.getPath());
				mobj.put("uploadTime", DateUtil.dateToString(files.getUploadTime(),DateUtil.FORMAT_LONG) );
				mobj.put("uploadUserName", usersService.getUserName(files.getUploadUserId()));
				jarr.add(mobj);
			}	
		}
		json.put("rows", jarr);
		totalCount = filesService.getTotalCount( params,  new String[] {"fileName"},  new String[] {"deviceKey"});
		json.put(APP.DATA, jarr);
		json.put(APP.TOTAL_COUNT, totalCount);
		json.put(APP.POS, posStart);
		return json;
	}

	@RequestMapping("files/upload")
	@ResponseBody
	public JSONObject upload(HttpServletRequest request, String id, String typeId) {
		return filesService.upload(request,id,typeId);
	}

	@RequestMapping("files/uploadFolder")
	@ResponseBody
	public JSONObject uploadFolder(HttpServletRequest request) {
		return filesService.uploadFolder(request);
	}

	/**
	 * 文件下载
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "files/download")
	public void download(@RequestBody JSONObject data, HttpServletResponse response){
		filesService.download(data,response);
	}

	@RequestMapping(value = "files/preview")
	@ResponseBody
	public JSONObject preview(@RequestBody JSONObject data){
		return filesService.preview(data);
	}

	@RequestMapping("files/editName")
	@ResponseBody
	public JSONObject editName(@RequestBody JSONObject data) {
		return filesService.editName(data);
	}

	@RequestMapping("files/delete")
	@ResponseBody
	public JSONObject delete(@RequestBody JSONObject data) {
		return filesService.deleteFile(data);
	}

}
